
/*
 * Java class "AntiGrav.java" generated from Poseidon for UML.
 * Poseidon for UML is developed by <A HREF="http://www.gentleware.com">Gentleware</A>.
 * Generated with <A HREF="http://jakarta.apache.org/velocity/">velocity</A> template engine.
 */
package jcs;

import robocode.*;


/**
 * 
 * @poseidon-object-id [sm$5cb0bb:ffee7186d0:-7ff5]
 */
public class AntiGrav {

  ///////////////////////////////////////
  // attributes


/**
 * 
 * @poseidon-object-id [sm$5cb0bb:ffee7186d0:-7fef]
 */
    private final double WALLFACTOR = 6000.0; 

/**
 * 
 * @poseidon-object-id [sm$5cb0bb:ffee7186d0:-7fea]
 */
    private final double BOTFACTOR = 15000.0; 

/**
 * 
 * @poseidon-object-id [sm$5cb0bb:ffee7186d0:-7fe5]
 */
    private final double WALL_TOLERANCE = 3000.0; 

/**
 * 
 * @poseidon-object-id [sm$5cb0bb:ffee7186d0:-7fe0]
 */
    private double xWall; 

/**
 * 
 * @poseidon-object-id [sm$5cb0bb:ffee7186d0:-7fdc]
 */
    private double yWall; 

/**
 * <p>Represents ...</p>
 * @poseidon-object-id [sm$3cc05f:ffee92588c:-7d85]
 */
    private final int POINT_POWER = 3; 

   ///////////////////////////////////////
   // associations

/**
 * <p></p>
 * @poseidon-object-id [sm$3040c5:ffee75eaf6:-7f67]
 */
    public Seth seth; 


  ///////////////////////////////////////
  // operations


/**
 * 
 * 
 * @poseidon-object-id [sm$5cb0bb:ffee7186d0:-7fd8]
 * @return 
 * @param _seth 
 */
    public  AntiGrav(Seth _seth) {        
        		 xWall = _seth.getBattleFieldWidth();
        		 yWall = _seth.getBattleFieldHeight();
        		 seth = _seth;
    } // end AntiGrav        

/**
 * 
 * 
 * @poseidon-object-id [sm$5cb0bb:ffee7186d0:-7fd2]
 * @return 
 * @param currentPos 
 */
    public Coord calcCoord(Coord currentPos) {        
              		Enemy[] enemies = seth.getEnemyList().getEnemies();
              		Coord go = new Coord(0,0);
              		double hyp =  0.0;
              		double xSum =  0.0;
              		double ySum =  0.0;
              		// Calculate the walls
              		Coord wall = wallSum(currentPos);
              				
              		// Calculate the bots
              		double botX =  0.0;
              		double botY =  0.0;
              		Coord temp;
              		for(int i = 0; i< enemies.length && enemies[i] != null; i++)
              		{
              			temp = pointSum(currentPos, enemies[i].getCoord(), 1);
              			botX += temp.getX();
              			botY += temp.getY();
              		}
              		
                        // Artificial Gravity Points
                        Coord artificial = pointSum( currentPos, new Coord(xWall/2.0, yWall/2.0), 0.5 );
                        
              		// Dividing by number of bots so they do not overpower the walls
              		xSum = botX / seth.getEnemyList().getCount() + wall.getX() + artificial.getX();
              		ySum = botY / seth.getEnemyList().getCount() + wall.getY() + artificial.getY();
              		go.setX(xSum + currentPos.getX());
              		go.setY(ySum + currentPos.getY());
        
              		return go;
    } // end calcCoord        

/**
 * 
 * 
 * @poseidon-object-id [sm$5cb0bb:ffee7186d0:-7fcc]
 * @return 
 * @param currentPos 
 */
    private Coord wallSum(Coord currentPos) {        
        		double sumX =  0.0;
        		double sumY =  0.0;
        		
        		if( currentPos.getX() < WALL_TOLERANCE)
        		{
        			sumX += WALLFACTOR * (1 / currentPos.getX());
        		}
        		if( currentPos.getY() < WALL_TOLERANCE)
        		{
        			sumY += WALLFACTOR * (1 / currentPos.getY());
        		}
        		if( xWall - currentPos.getX() < WALL_TOLERANCE)
        		{
        			sumX -= WALLFACTOR * (1 / (xWall - currentPos.getX()));
        		}
        		if( yWall - currentPos.getY() < WALL_TOLERANCE)
        		{
        			sumY -= WALLFACTOR * (1 / (yWall - currentPos.getY()));
        		}		
        		return new Coord(sumX, sumY);
    } // end wallSum        

/**
 * 
 * 
 * @poseidon-object-id [sm$5cb0bb:ffee7186d0:-7fc6]
 * @return 
 * @param currentPos 
 * @param enemyPos 
 * @param power 
 */
    private Coord pointSum(Coord currentPos, Coord enemyPos, double power) {        
        		double distance = currentPos.calcDist(enemyPos);
        		double theta = enemyPos.calcHeading(currentPos);
        		double force = power * BOTFACTOR * (1 / distance);
        		Coord sum = currentPos.getCoordFromScan( theta, force );			
        		sum.setX( sum.getX() - currentPos.getX() );
        		sum.setY( sum.getY() - currentPos.getY() );
        		return sum;
    } // end pointSum        

 } // end AntiGrav


